<template>
    <div>
        门店名称<input type="text" v-model="queryinfo.shopname"/>
        服务范围
        <select v-model="queryinfo.fuwuid">
            <option value="">请选择</option>
            <option :value="item.Id" v-for="item in data.list1">{{ item.FuwuName }}</option>
        </select>
        <input type="button" value="查询" @click="show" class="btn btn-primary"/>
        <table class="table table-bordered">
            <tbody>
                <tr>
                    <input type="checkbox" v-model="checked" @click="checkall"/>
                    <td>Id</td>
                    <td>门店</td>
                    <td>地址</td>
                    <td>门店logo</td>
                    <td>门店图片</td>
                    <td>服务范围</td>
                    <td>店主</td>
                    <td>状态</td>
                    <td>联系电话</td>
                    <td>营业时间</td>
                    <td>操作</td>
                </tr>
                <tr v-for="item in data.list" :key="item.Id">
                    <input type="checkbox" v-model="ids" :key="item.id"/>
                    <td>{{ item.Id }}</td>
                    <td>{{ item.ShopName }}</td>
                    <td>{{ item.Adress }}</td>
                    <td><img :src="item.Logo" width="100" height="100" alt=""/></td>
                    <td><img :src="item.Photo" width="100" height="100" alt=""/></td>
                    <td>{{ item.FuwuId }}</td>
                    <td>{{ item.Dianzhu }}</td>
                    <td>{{ item.State?"启用":"禁用" }}</td>
                    <td>{{ item.Phone }}</td>
                    <td>{{ item.CreateTime.substring(0,10) }}</td>
                    <td><input type="button" value="修改" class="btn btn-success" @click="upd(item.Id)"/>
                        <input type="button" value="批量删除" @click="delall(item.Id)" class="btn btn-danger"/>
                        <input type="button" value="批量修改" @click="updall(item.Id)" class="btn btn-primary"/>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
    <div>
        <span>共<b>{{ data.topage }}</b>页</span>
        <span>每页<b>{{ queryinfo.pagesize }}</b>条</span>
        <span>共<b>{{ queryinfo.pageindex }}</b>/<b>{{ data.topage }}</b>页</span>
        <input type="button" value="首页" @click="fanye(1)" class="btn btn-info"/>
        <input type="button" value="上一页" @click="fanye(queryinfo.pageindex-1)" class="btn btn-info"/>
        <span :key="item" v-for="item in data.topage">{{ item }}</span>
        <input type="button" value="下一页" @click="fanye(queryinfo.pageindex+1)" class="btn btn-info"/>
        <input type="button" value="尾页" @click="fanye(data.topage)" class="btn btn-info"/>
    </div>
</template>

<script setup lang="ts">
import {ref,reactive, onMounted} from 'vue'
import axios from 'axios';
import moment from 'moment';
import {useRouter,useRoute}from 'vue-router'
const router=useRouter();
const route=useRoute();

let data:any=reactive({
    list:[],
    tocount:0,
    topage:0,
    list1:[],
})

let checked=ref(false);
let ids=ref([]);

let queryinfo:any=reactive({
    pageindex:1,
    pagesize:2,
    shopname:'',
    fuwuid:'',
})

onMounted(()=>{
    getlist();
    getbang();
})

const upd=(id:any)=>{
    router.push({
        path:'/shopupd',
        query:{id:id}
    })
}

const updall=(id:any)=>{
    if(!confirm("确认修改吗")){
        return;
    }
    axios({
        url:'/api/Shop/Updashopall',
        method:'put',
        params:{id:id}
    })
    .then(res=>{
        console.log(res);
        if(res.data>0){
            alert('修改成功');
            checked.value=false;
            show();
        }
        else{
            alert('修改失败');
        }
    })
    .catch(err=>{
        console.log(err);
    })
}

const delall=(id:any)=>{
    if(!confirm("确认删除吗")){
        return;
    }
    axios({
        url:'/api/Shop/Deleteshop',
        method:'delete',
        params:{id:id}
    })
    .then(res=>{
        console.log(res);
        if(res.data>0){
            alert('删除成功');
            checked.value=false;
            show();
        }
        else{
            alert('删除失败');
        }
    })
    .catch(err=>{
        console.log(err);
    })
}

const fanye=(num:any)=>{
    if(num<1){
        queryinfo.pageindex=1;
        return;
    }
    if(num>data.topage){
        queryinfo.pageindex=data.topage;
        return;
    }
    queryinfo.pageindex=num;
    getlist();
}

const checkall=()=>{
    checked.value=!checked.value;
    if(checked.value==true){
        ids.value=data.list.map((item:any)=>{
            item.id;
        })
    }
    else{
        return ids.value=[];
    }
}

const show=()=>{
    queryinfo.pageindex=1;
    getlist();
}

const getbang=()=>{
    axios({
        url:'/api/Shop/Getfuwu',
        method:'get',
    })
    .then(res=>{
        console.log(res);
        data.list1=res.data;
    })
    .catch(err=>{
        console.log(err);
    })
}
const getlist=()=>{
    axios({
        url:'/api/Shop/GetShopList',
        method:'get',
        params:queryinfo
    })
    .then(res=>{
        console.log(res);
        data.list=res.data.list;
        data.tocount=res.data.tocount;
        data.topage=res.data.topage;
    })
    .catch(err=>{
        console.log(err);
    })
}
</script>

<style scoped>
b{
    color: red;
}
span{
    margin-left: 10px;
}
input[type="button"]{
    margin-left: 10px;
}
</style>